﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Collections;
using System.Xml;
using System.IO;

namespace BASY
{
    public partial class Form1 : Form
    {
        #region 成员变量
        //打印页面
        private PrintDocument printDocument = null;

        private double zoom = 0.5;
        private const double defaultzoom = 0.5;
        //定义字体
        private Font previewfont = new Font("宋体", 10);//打印预览正文字体
        private Font previewTitlefont = new Font("宋体", 15);//打印预览标题字体
        private Brush brushCellFont = new SolidBrush(Color.Black);

        private Font printfont = new Font("宋体", 15);//打印正文字体
        private Font printTitlefont = new Font("宋体", 25);//打印标题字体
        Font f = new System.Drawing.Font("宋体", 11);
        //定义画笔
        private Pen framePen = new Pen(Color.Black);
        private Pen drawlinenPen = new Pen(Color.Black);
        //文本
        private string title = null;

        //制表日期
        private string createtime = string.Empty;
        //报表统计日期
        private string statisticalRange = string.Empty;

        //设置打印数据
        private int startPointX = 60;
        private int startPointY = 60;
        private int defaultWidth = 840;
        private int defaultCellHeight = 40;

        //调整表格内高度
        private int offestHeight = 1;

        //打印配置
        private double configZoom = 0.00;

        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        #region 打印
        public void Print()
        {
            printDocument = new System.Drawing.Printing.PrintDocument();
            printPreviewDialog1.Document = printDocument;
            //case1
            //// printDocument.DefaultPageSettings.PaperSize = new PaperSize("A4", 1169, 827);
            //文档旋转
            //printDocument.DefaultPageSettings.Landscape = true;
            //打印机转
            // printDocument.DefaultPageSettings.PrinterSettings.DefaultPageSettings.Landscape = true;
            printDialog1.PrinterSettings.DefaultPageSettings.Landscape = true;
            printDocument.DefaultPageSettings.Landscape = true;

            printDocument.PrintPage += new PrintPageEventHandler(this.printdocument_printpage);//事件的重载
            //printDocument.Print();
        }

        private void printdocument_printpage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            SetInitForPrint();
            zoom = 1 * configZoom;
            //打印
            PanelAndDocumentPaint(e.Graphics);

        }
        #endregion


        private void SetInitForPrint()
        {
            this.InitPrintFont();
        }

        private void InitPrintFont()
        {
            previewfont = printfont;
            previewTitlefont = printTitlefont;
        }


        private int GetCenter(int rectLength, int textLength)
        {
            int offest = 0;
            if (rectLength > textLength)
            {
                offest = (rectLength - textLength) / 2;
            }
            return offest;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //if (File.Exists("病案首页.xml"))
            //{
            //    XmlDocument xmldoc = new XmlDocument();
            //    xmldoc.Load("病案首页.xml");
            //    XmlNode nodeTop = xmldoc.SelectSingleNode("PrintSetting/Margins/Top");
            //    XmlNode nodeBottom = xmldoc.SelectSingleNode("PrintSetting/Margins/Bottom");
            //    XmlNode nodeLeft = xmldoc.SelectSingleNode("PrintSetting/Margins/Left");
            //    XmlNode nodeRight = xmldoc.SelectSingleNode("PrintSetting/Margins/Right");
            //}

        }

        private void PanelAndDocumentPaint(Graphics g)
        {
            int linewidth = Convert.ToInt32(0.8 * 40 * zoom);
            int x = Convert.ToInt32(26 * 40 * zoom);
            int originalX = Convert.ToInt32(startPointX * zoom);
            int originalY = Convert.ToInt32(startPointY * zoom);
            int originalwidth = Convert.ToInt32((defaultWidth - 120) * 2 * zoom);
            int zoomWeight = Convert.ToInt32(20 * zoom);
            int zoomHeight = Convert.ToInt32(defaultCellHeight * zoom);
            int strWidth = (int)(g.MeasureString(title, previewTitlefont).Width);
            int drawX = originalX + this.GetCenter(originalwidth, strWidth);
            g.DrawString(title, previewTitlefont, brushCellFont, new PointF((int)drawX, originalY));

            Rectangle rect = new Rectangle(new Point(originalX, originalY), new Size(linewidth, zoomHeight * 2));
            g.DrawRectangle(drawlinenPen, rect);

            x = linewidth + x;
            linewidth = Convert.ToInt32(0.8 * 40 * zoom);
            rect = new Rectangle(new Point(x, originalY), new Size(linewidth, zoomHeight * 2));
            g.DrawRectangle(drawlinenPen, rect);

            strWidth = (int)(g.MeasureString(null, previewfont).Width);
            drawX = x + this.GetCenter(linewidth, strWidth);
            g.DrawString(null, previewfont, brushCellFont, new PointF(drawX, originalY + offestHeight * 2));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Print();
            printDocument.Print();
        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            PanelAndDocumentPaint(e.Graphics);
        }

        //private void button1_Click(object sender, EventArgs e)
        //{

        //    try
        //    {

        //        int num = int.Parse(textBox1.Text);

        //        for (int i = 0; i < num; i++)
        //        {

        //            TextBox tb = new TextBox();

        //            //设定位置

        //            tb.Top = 50 + i * 30;

        //            //添加控件

        //            this.Controls.Add(tb);

        //        }

        //    }

        //    catch (Exception ex)
        //    {

        //        MessageBox.Show(ex.Message); ;

        //    }

        //}


    }
}
